﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using BOHVote.BLL;

namespace BOH.DataManagement
{
    public partial class TestTypeList : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                TestTypeBind();
            }
        }

        #region <<  绑定列表  >>
        public void TestTypeBind()
        {
            DataSet dst = ApplyTestClass.getAllTestType();

            BindTestTypeList(dst);
        }

        public void BindTestTypeList(DataSet dst)
        {
            AspNetPager1.RecordCount = dst.Tables[0].Rows.Count;
            if (AspNetPager1.RecordCount != 0)
            {
                labNoRecord.Visible = false;
                AspNetPager1.Visible = true;
                PagedDataSource pds = new PagedDataSource();
                pds.DataSource = dst.Tables[0].DefaultView;
                pds.AllowPaging = true;
                pds.CurrentPageIndex = AspNetPager1.CurrentPageIndex - 1;
                pds.PageSize = AspNetPager1.PageSize;
                gvTestTypeList.DataSource = pds;
                gvTestTypeList.DataBind();
            }
            else
            {
                gvTestTypeList.DataSource = null;
                gvTestTypeList.DataBind();
                AspNetPager1.Visible = false;
                labNoRecord.Visible = true;
            }
        }

        protected void AspNetPager1_PageChanged(object src, EventArgs e)
        {
            TestTypeBind();
        }
        #endregion



        public string getStatus(string status)
        {
            string returnvalue = string.Empty;
            switch (status.Trim())
            {
                case "A":
                    returnvalue = "激活";
                    break;
                case "D":
                    returnvalue = "<font color='red'>未激活</font>";
                    break;
                default:
                    break;
            }

            return returnvalue;
        }

        protected void gvTestTypeList_RowEditing(object sender, GridViewEditEventArgs e)
        {
            gvTestTypeList.EditIndex = e.NewEditIndex;
            TestTypeBind();
        }

        protected void gvTestTypeList_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow && (e.Row.RowState == DataControlRowState.Edit||e.Row.RowState == (DataControlRowState.Alternate | DataControlRowState.Edit)))
            {
                DropDownList drpstatus = (DropDownList)e.Row.FindControl("drpStatus");
                drpstatus.SelectedValue = ((HiddenField)e.Row.FindControl("hideStatus")).Value.Trim() ;
            }
        }

        protected void gvTestTypeList_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            gvTestTypeList.EditIndex = -1;
            TestTypeBind();
        }

        protected void gvTestTypeList_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            string TestTypeID = string.Empty;
            string TestTypeName = string.Empty;
            string status = string.Empty;
            GridViewRow gvr = gvTestTypeList.Rows[e.RowIndex];
            TestTypeID = ((Label)gvr.FindControl("labTestTypeID")).Text;
            TestTypeName = ((TextBox)gvr.FindControl("txtTypeName")).Text;
            status = ((DropDownList)gvr.FindControl("drpStatus")).SelectedValue;
            ApplyTestClass.UpdateTestType(TestTypeName, status, TestTypeID);

            gvTestTypeList.EditIndex = -1;
            TestTypeBind();
        }

        protected void butDelete_Click(object sender, EventArgs e)
        {
            if (hiddenSelected.Value.Trim().Length > 0 && hiddenSelected.Value.Trim() != ",")
            {
                string[] arrTestTypeID = hiddenSelected.Value.Trim().Split(new char[] { ',' });
                foreach (string TestTypeID in arrTestTypeID)
                {
                    if (TestTypeID.Trim().Length > 0)
                    {
                        ApplyTestClass.DeleteTestType(TestTypeID);
                    }
                }
                TestTypeBind();
            }
        }
    }
}
